package com.ruoyi.factory.taxShare.utils;


import nccloud.open.api.auto.token.cur.utils.APICurUtils;
import nccloud.open.api.auto.token.itf.IAPIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName OpenApiUtils
 * @Description: TODO openapi 请求工具类
 * @Author LiuJiaji
 * @Date 2023/12/12 13:53
 **/
@Component
public class OpenApiUtils {

    protected static final Logger logger = LoggerFactory.getLogger(OpenApiUtils.class);

    @Value("${erp.openapi.ip}")
    private String cIp;
    @Value("${erp.openapi.port}")
    private String cPort;
    @Value("${erp.openapi.busi_center}")
    private String cBusiCenter;
    @Value("${erp.openapi.app_id}")
    private String cAppId;
    @Value("${erp.openapi.app_secret}")
    private String cAppSecret;
    @Value("${erp.openapi.public_key}")
    private String cPublicKey;
    @Value("${erp.openapi.ncc_user}")
    private String cNccUser;
    @Value("${erp.openapi.password}")
    private String cPassword;

    private static String openApiIp;
    private static String openApiProt;
    private static String openApiBusiCenter;
    private static String openApiAppId;
    private static String openApiAppSecret;
    private static String openApiPublicKey;
    private static String openApiNccUser;
    private static String openApiPassword;

    @PostConstruct
    public void getConfig(){
        openApiIp = this.cIp;
        openApiProt = this.cPort;
        openApiBusiCenter = this.cBusiCenter;
        openApiAppId = this.cAppId;
        openApiAppSecret = this.cAppSecret;
        openApiPublicKey = this.cPublicKey;
        openApiNccUser = this.cNccUser;
        openApiPassword = this.cPassword;
    }

    public static String openApiSend(String url, String param,String desc){
        IAPIUtils util = null;
        //		util = new APIOldUtils(); // 2021.05之前版本
        util = new APICurUtils();// 2021.05版本及以后版本
        // 服务地址ip
        String ip = openApiIp;
        // 服务端口号
//        String port = "8087";
        String port = openApiProt;
        // 账套编码，开发环境使用develop，生产环境需要使用用root账号登录，在“系统管理”节点查看账套编码
        String busi_center = openApiBusiCenter;
        // 第三方应用编码
        String app_id = openApiAppId;
        // app_secret，用于请求加签
        String app_secret = openApiAppSecret;
        // 公钥，加解密使用
        String public_key = openApiPublicKey;
        // ncc用户名
        String ncc_user = openApiNccUser;
        // ncc用户密码
        String password = openApiPassword;

        String grant_type = "client";
        util.init(ip, port, busi_center, app_id, app_secret, public_key, ncc_user, password);
        util.setGrant_type(grant_type);
        // 获取token
        String token = null;
        String result = "";
        try {
            token = util.getToken();
        logger.info("请求openapi获取token"+token);
        util.setApiUrl(url);
        logger.info("请求openapi"+desc+"地址: "+url);
        logger.info("请求openapi"+desc+"入参: "+param);
        // 携带token访问接口，获取结果
        result = util.getAPIRetrun(token, param);
        } catch (Exception e) {
            logger.info("请求openapi报错信息: "+e.getMessage());
        }
        logger.info("请求openapi"+desc+"返参: "+result);
        return result;
    }
}
